package com.greenold.commen.service.impl;

import com.greenold.commen.domain.Dto.ElderDto;
import com.greenold.commen.domain.Elder;
import com.greenold.commen.mapper.ElderMapper;
import com.greenold.commen.service.ElderService;
import com.greenold.common.core.domain.AjaxResult;
import com.greenold.common.core.domain.entity.SysUser;
import com.greenold.common.utils.SecurityUtils;
import com.greenold.system.domain.WxChatCustomer;
import com.greenold.system.mapper.SysUserMapper;
import com.greenold.system.mapper.WxChatCustomerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ElderServiceImpl implements ElderService {
    @Autowired
    private ElderMapper elderMapper;
    @Autowired
    private WxChatCustomerMapper wxChatCustomerMapper;


    @Override
    public List<Elder> selectElderListCheckInByElderId(){
        return elderMapper.selectElderListCheckIn();
    }
    @Override
    public List<ElderDto> selectElderDtoList() {
        return  elderMapper.selectElderListDto();
    }
    @Override
    public AjaxResult getAllElder() {
        AjaxResult ajax = new AjaxResult();
        String userId = SecurityUtils.getUserId().toString();
        WxChatCustomer wxChatCustomer = wxChatCustomerMapper.selectWxChatCustomerByUserId(userId);
        if(wxChatCustomer == null)
        {
            return ajax.error("微信绑定不存在");
        }
        List<Elder> list = new ArrayList<>();
        List<Integer> ElderIds = wxChatCustomerMapper.selectElderIdsById(wxChatCustomer.getId().intValue());
        for(Integer ElderId:ElderIds)
        {
            Elder elder = elderMapper.selectElderCheckInById(ElderId.toString());
            if(elder == null)
            {
                continue;
            }
            list.add(elder);
        }
        return ajax.success(list);
    }
    @Override
    public String getCustomerId() {
        try {
            // 1. 获取当前登录用户ID（若未登录，SecurityUtils会抛异常，这里捕获处理）
            String userId = SecurityUtils.getUserId().toString();
            System.out.println("当前登录用户ID：" + userId);

            // 2. 根据用户ID查询微信客户关联记录
            WxChatCustomer wxChatCustomer = wxChatCustomerMapper.selectWxChatCustomerByUserId(userId);
            System.out.println("查询到的微信客户记录：" + (wxChatCustomer == null ? "无" : wxChatCustomer.toString()));

            // 3. 处理查询结果（返回ID或明确提示）
            if (wxChatCustomer == null) {
                System.out.println("警告：用户ID=" + userId + " 未绑定微信客户");
                return null; // 前端会处理空值
            }
            if (wxChatCustomer.getId() == null) {
                System.out.println("警告：微信客户记录存在，但ID为空");
                return null;
            }
            return wxChatCustomer.getId().toString();
        } catch (Exception e) {
            // 捕获所有异常（如未登录、查询失败等）
            System.err.println("获取客户ID失败：" + e.getMessage());
            e.printStackTrace();
            return null;
        }
    }



}
